using System;

namespace Fornax020.SerialComm.Protocol
{
    /// <summary>
    /// 串口通信协议常量定义
    /// 统一管理所有协议相关的常量，避免魔术字符串和数字
    /// </summary>
    public static class ProtocolConstants
    {
        #region 协议控制字符
        /// <summary>
        /// 起始字符 (STX)
        /// </summary>
        public const char STX = (char)0x02;
        
        /// <summary>
        /// 回车符 (CR)
        /// </summary>
        public const char CR = (char)0x0D;
        
        /// <summary>
        /// 空格符 (SP)
        /// </summary>
        public const char SP = (char)0x20;
        
        /// <summary>
        /// ASCII字符'0'
        /// </summary>
        public const char ASCII_0 = '0';
        #endregion

        #region 命令类型常量
        /// <summary>
        /// 命令类型常量
        /// </summary>
        public static class CommandTypes
        {
            /// <summary>
            /// KV值反馈
            /// </summary>
            public const string KV_FEEDBACK = "VP";
            
            /// <summary>
            /// MA值反馈
            /// </summary>
            public const string MA_FEEDBACK = "CP";
            
            /// <summary>
            /// FIL值反馈
            /// </summary>
            public const string FIL_FEEDBACK = "FP";
            
            /// <summary>
            /// 监控数据
            /// </summary>
            public const string MONITOR_DATA = "MO";
            
            /// <summary>
            /// 固件版本
            /// </summary>
            public const string FIRMWARE_VERSION = "GR";
            
            /// <summary>
            /// 错误信息
            /// </summary>
            public const string ERROR_INFO = "ER";
            
            /// <summary>
            /// 开始曝光响应
            /// </summary>
            public const string START_EXPOSURE_RESPONSE = "EN1";
            
            /// <summary>
            /// 停止曝光响应
            /// </summary>
            public const string STOP_EXPOSURE_RESPONSE = "EN0";
            
            /// <summary>
            /// 清除错误响应
            /// </summary>
            public const string CLEAR_ERROR_RESPONSE = "CL";
        }
        #endregion

        #region 数据包长度常量
        /// <summary>
        /// 数据包长度常量
        /// </summary>
        public static class PacketLength
        {
            /// <summary>
            /// KV反馈包长度
            /// </summary>
            public const int KV_FEEDBACK = 8;
            
            /// <summary>
            /// MA反馈包长度
            /// </summary>
            public const int MA_FEEDBACK = 8;
            
            /// <summary>
            /// FIL反馈包长度
            /// </summary>
            public const int FIL_FEEDBACK = 7;
            
            /// <summary>
            /// 监控数据包长度
            /// </summary>
            public const int MONITOR_DATA = 19;
            
            /// <summary>
            /// 固件版本包长度
            /// </summary>
            public const int FIRMWARE_VERSION = 15;
            
            /// <summary>
            /// 错误信息包长度
            /// </summary>
            public const int ERROR_INFO = 7;
            
            /// <summary>
            /// 开始曝光响应包长度
            /// </summary>
            public const int START_EXPOSURE_RESPONSE = 5;
            
            /// <summary>
            /// 停止曝光响应包长度
            /// </summary>
            public const int STOP_EXPOSURE_RESPONSE = 5;
            
            /// <summary>
            /// 清除错误响应包长度
            /// </summary>
            public const int CLEAR_ERROR_RESPONSE = 4;
        }
        #endregion

        #region 命令格式常量
        /// <summary>
        /// 命令格式常量
        /// </summary>
        public static class CommandFormats
        {
            /// <summary>
            /// KV设置命令格式
            /// </summary>
            public const string SET_KV = "SKV";
            
            /// <summary>
            /// MA设置命令格式
            /// </summary>
            public const string SET_MA = "SMA";
            
            /// <summary>
            /// 开始曝光命令格式
            /// </summary>
            public const string START_EXPOSURE = "SEN";
            
            /// <summary>
            /// 停止曝光命令格式
            /// </summary>
            public const string STOP_EXPOSURE = "SST";
            
            /// <summary>
            /// 监控查询命令格式
            /// </summary>
            public const string MONITOR_QUERY = "SMO";
            
            /// <summary>
            /// 版本查询命令格式
            /// </summary>
            public const string VERSION_QUERY = "SGR";
            
            /// <summary>
            /// 清除错误命令格式
            /// </summary>
            public const string CLEAR_ERROR = "SCL";
        }
        #endregion

        #region 错误描述
        /// <summary>
        /// 获取错误描述
        /// </summary>
        /// <param name="errorCode">错误代码</param>
        /// <returns>错误描述</returns>
        public static string GetErrorDescription(string errorCode)
        {
            switch (errorCode)
            {
                case "E01":
                    return "设备未连接";
                case "E02":
                    return "参数超出范围";
                case "E03":
                    return "设备忙";
                case "E04":
                    return "通信超时";
                case "E05":
                    return "数据格式错误";
                case "E06":
                    return "设备故障";
                case "E07":
                    return "权限不足";
                case "E08":
                    return "未知错误";
                default:
                    return $"未知错误代码: {errorCode}";
            }
        }
        #endregion
    }
}
